//
//  AddBankCardViewController.m
//  Kjs-iOS
//
//  Created by 江伟 on 16/6/28.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "AddBankCardViewController.h"
#import "SelectBankCardViewController.h"
#import "UIImage+MapUIImage.h"
#import "CommonUtil.h"
#import "KJSRequest.h"
#import "UIColor+KjsColor.h"
#import "AppDelegate.h"

#define CodeTimeInterval 180
@interface AddBankCardViewController ()
@property (nonatomic,weak) IBOutlet UIButton *codeBtn;
@property (nonatomic,weak) IBOutlet UIButton *confirmBtn;
@property (nonatomic,weak) IBOutlet UILabel *accountNameLab;
@property (nonatomic,weak) IBOutlet UIImageView *rightArrowIV;
@property (nonatomic,weak) IBOutlet UITextView *tipTV;
@property (nonatomic,weak) IBOutlet UIView *selectBankCardView;

@property (nonatomic,weak) IBOutlet UILabel *bankCardNameLab;
@property (nonatomic,weak) IBOutlet UITextField *codeTF;
@property (nonatomic,weak) IBOutlet UITextField *bankCardNumTF;
@property (nonatomic,weak) IBOutlet UITextField *bankPhoneTF;
@property (nonatomic,weak) IBOutlet UILabel *userNameLab;

@property (nonatomic,assign) NSInteger bankCardNameTpye;

@property (nonatomic,assign) int secondsCountDown;//倒计时总时长
@property (nonatomic,strong) NSTimer *countDownTimer;
@end

@implementation AddBankCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem setTitle:@"添加银行卡"];
    self.confirmBtn.layer.cornerRadius = 10;
    self.codeBtn.layer.cornerRadius = 15;
    self.accountNameLab.text = @"*雅文";
    self.rightArrowIV.image = [UIImage drawArrowWithDirection:DirectionTypeRight];
    self.rightArrowIV.userInteractionEnabled = YES;
    self.selectBankCardView.userInteractionEnabled = YES;
    UITapGestureRecognizer *goSelectBank = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goSelectBank)];
    [self.rightArrowIV addGestureRecognizer:goSelectBank];
    [self.selectBankCardView addGestureRecognizer:goSelectBank];
    self.tipTV.userInteractionEnabled = NO;
    self.secondsCountDown = CodeTimeInterval;
    
    [self.codeBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    [self.confirmBtn addTarget:self action:@selector(addBankCard) forControlEvents:UIControlEventTouchUpInside];

    [self initConfiguration];


}
#pragma mark - custom method
- (void) goSelectBank
{
    NSLog(@"跳转到选择银行卡节目");
    SelectBankCardViewController *selectBankCardVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"SelectBankCardViewController"];
    selectBankCardVC.selectBankCard = ^(NSInteger bankType){
        self.bankCardNameLab.text = bankNameArray[bankType];
        self.bankCardNameTpye = bankType;
    };
    [self displayViewController:selectBankCardVC];
}

-(void)addBankCard
{
    if (self.bankCardNameTpye == -1){
        [GUATool showDialog:@"请选择发卡银行" message:nil waitingTime:1.5];
    } else if ([self.bankCardNumTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入银行卡号" message:nil waitingTime:1.5];
    }else if (![self checkCardNo:self.bankCardNumTF.text]){
        [GUATool showDialog:@"请输入正确的银行卡号" message:nil waitingTime:1.5];
    }else if ([self.bankPhoneTF.text isEqualToString:@""]){
        [GUATool showDialog:@"请输入预留手机号" message:nil waitingTime:1.5];
    }else if ([self.codeTF.text isEqualToString:@""]){
        [GUATool showDialog:@"请输入验证码" message:nil waitingTime:1.5];
    }else{
        if (CurentDelegate.account.partyId) {
            [KJSRequest AddBankCardWithPartyId:CurentDelegate.account.partyId verificationCode:self.codeTF.text bankCardNameType:self.bankCardNameTpye bankCardNum:self.bankCardNumTF.text bankCardPhone:self.bankPhoneTF.text ret:^(BasicResult *model) {
                if (model.success == true) {
                    [GUATool showDialog:model.msg message:nil waitingTime:1.5];
                    if (self.addSuccess) {
                        self.addSuccess();
                    }
                    //修改用户等级
                    if ([CurentDelegate.account.levels integerValue] == 2) {
                        CurentDelegate.account.levels = @"3";
                    }
                    [self initConfiguration];
                    [self dismiss:YES];
                }
            }];

        }
    }
    
    
}
-(void)sendMessage
{
    self.confirmBtn.enabled = YES;
    [self.confirmBtn setBackgroundColor:[UIColor kjsMainColor]];
    if ([self.bankPhoneTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入银行预留手机号" message:nil waitingTime:1.5];
    }else if (CurentDelegate.account.nickName) {
        [KJSRequest AddBankCardSendMessageWithBankCardPhone:self.bankPhoneTF.text sendName:CurentDelegate.account.nickName ret:^(BasicResult *model) {
            if([model.messageresult isEqualToString:@"0"]) {
                [GUATool showDialog:@"验证码已发送" message:nil waitingTime:1.5];
                self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
            }
        }];
    }
}

-(void)initConfiguration
{
    self.bankPhoneTF.text = @"";
    self.bankCardNumTF.text = @"";
    self.codeTF.text = @"";
    self.confirmBtn.enabled = NO;
    self.bankCardNameLab.text = @"选择发卡银行";
    self.bankCardNameTpye = -1;
    [self.confirmBtn setBackgroundColor:[UIColor lightGrayColor]];
    self.userNameLab.text = CurentDelegate.account.nickName;
}
-(void)timeFireMethod{
    
    self.secondsCountDown--;
    self.codeBtn.titleLabel.text = [NSString stringWithFormat:@"%d秒",self.secondsCountDown];
    [self.codeBtn setTitle:[NSString stringWithFormat:@"%d秒",self.secondsCountDown] forState:UIControlStateNormal];
    self.codeBtn.enabled = NO;
    if (self.secondsCountDown == 0) {
        [self.countDownTimer invalidate];
        [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.codeBtn.enabled = YES;
        self.secondsCountDown = CodeTimeInterval;
    }
}
//验证银行卡
- (BOOL) checkCardNo:(NSString*) cardNo{
    int oddsum = 0;     //奇数求和
    int evensum = 0;    //偶数求和
    int allsum = 0;
    int cardNoLength = (int)[cardNo length];
    int lastNum = [[cardNo substringFromIndex:cardNoLength-1] intValue];
    
    cardNo = [cardNo substringToIndex:cardNoLength - 1];
    for (int i = cardNoLength -1 ; i>=1;i--) {
        NSString *tmpString = [cardNo substringWithRange:NSMakeRange(i-1, 1)];
        int tmpVal = [tmpString intValue];
        if (cardNoLength % 2 ==1 ) {
            if((i % 2) == 0){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }else{
            if((i % 2) == 1){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }
    }
    
    allsum = oddsum + evensum;
    allsum += lastNum;
    if((allsum % 10) == 0)
        return YES;
    else
        return NO;
}
@end
